﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Tools.Maptools.Editor;

[CustomEditor(typeof(TrafficNode))]
public class TrafficNodeEditor : Editor
{
    private TrafficNode _trafficNode;

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        _trafficNode = (TrafficNode)target;
        GUILayout.BeginVertical();
        for (int y = 0; y < _trafficNode.roadLineNum + 2; y++)
        {
            GUILayout.BeginHorizontal();
            for (int x = 0; x < _trafficNode.roadLineNum + 2; x++)
            {
                GUILayout.Space(30);
                if (x == y || Mathf.Abs(x - y) == _trafficNode.roadLineNum + 1)
                {
                    GUILayout.Space(100);
                    continue;// 对角线去除
                }
                if (x != 0 && x != _trafficNode.roadLineNum + 1 && y != 0 && y != _trafficNode.roadLineNum + 1) // 去除中间点
                {
                    GUILayout.Space(100);
                    continue;
                }
                int pointId = _trafficNode.GetTrafficPointId(x, y);
                GameObject pointObj = null;
                TrafficPoint point = _trafficNode.GetTrafficPoint(pointId);
                if (point) pointObj = point.gameObject;
                 GameObject newPointObj = EditorGUILayout.ObjectField(pointObj, typeof(GameObject), true, GUILayout.Width(100)) as GameObject;
                if (newPointObj && newPointObj != pointObj)
                {
                    if (pointObj)
                    {
                        TrafficPoint oldPoint = pointObj.GetComponent<TrafficPoint>();
                        oldPoint.SetPointId(0);
                    }
               
                    TrafficPoint newPoint = newPointObj.GetComponent<TrafficPoint>();
                    newPoint.SetPointId(pointId);
                    _trafficNode.SetTrafficPoint(pointId, newPoint);
                }
            }
            GUILayout.EndHorizontal();
        }
        if (GUILayout.Button("生成坐标点"))
        {
            _trafficNode.CreateTrafficPoint();
        }
        GUILayout.EndVertical();
    }
}

